package com.service.database.action;

import com.service.database.handler.AbstractDatabaseHandler;
import com.service.database.config.DataBase;
import com.service.database.constants.JdbcConstants;

/**
 * Url组装
 * @author xianggj
 * @Date 2021/6/4 15:38
 **/
public class UrlAssembler {

    private UrlAssembler() {
    }

    /**
     * jdbc前缀
     */
    private String perfix;

    private String host;

    private String port;

    private AbstractDatabaseHandler handler;

    /**
     * 创建 url
     *  这里只能满足大部分数据库，极个别数据库不按套路来，到时候去根据条件判断修改
     * @return
     */
    public String createUrl(){
        StringBuffer currentUrl = new StringBuffer(perfix + host + port);
        if (handler == null){
            return currentUrl.toString();
        }
        return handler.urlSpecialDeal(currentUrl);
    }

    /**
     * 类创建器
     */
    public static final class UrlAssemblerBuilder {
        private String host= "";
        private String port= "";
        private String dbName= "";
        private String schema = "";
        private String userName = "";
        private String pwd = "";
        private DataBase dataBase = DataBase.MYSQL;


        private UrlAssemblerBuilder() {
        }

        public static UrlAssemblerBuilder anUrlAssembler() {
            return new UrlAssemblerBuilder();
        }

        public UrlAssemblerBuilder withDataBase(DataBase dataBase) {
            this.dataBase = dataBase;
            return this;
        }

        public UrlAssemblerBuilder withDataBase(String dataBasestr) {
            this.dataBase = DataBase.toDataBase(dataBasestr);
            return this;
        }

        public UrlAssemblerBuilder withHost(String host) {
            this.host = host;
            return this;
        }

        public UrlAssemblerBuilder withPort(Integer port) {
            this.port = JdbcConstants.COLON_MARK + port + JdbcConstants.DECOLLATOR;
            return this;
        }

        public UrlAssemblerBuilder withDbName(String dbName) {
            this.dbName =  dbName;
            return this;
        }

        public UrlAssemblerBuilder withSchema(String schema) {
            this.schema = schema;
            return this;
        }

        public UrlAssemblerBuilder withUserName(String userName) {
            this.userName = userName;
            return this;
        }

        public UrlAssemblerBuilder withPwd(String pwd) {
            this.pwd =  pwd;
            return this;
        }

        public UrlAssembler build() {
            UrlAssembler urlAssembler = new UrlAssembler();
            urlAssembler.perfix = this.dataBase.getJdbcPrefix();
            urlAssembler.host = this.host;
            urlAssembler.port = this.port;
            AbstractDatabaseHandler handler = this.dataBase.getHandler();
            handler.setDbName(this.dbName);
            handler.setPwd(this.pwd);
            handler.setSchema(this.schema);
            handler.setUserName(this.userName);
            urlAssembler.handler = handler;
            return urlAssembler;
        }
    }
}
